package com.situ.crm.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.crm.VO.LayUISelectMVO;
import com.situ.crm.common.ServerResponse;
import com.situ.crm.entity.Order;
import com.situ.crm.entity.Permission;
import com.situ.crm.entity.SaleChance;
import com.situ.crm.mapper.OrderMapper;
import com.situ.crm.mapper.PermissionMapper;
import com.situ.crm.mapper.SaleChanceMapper;
import com.situ.crm.service.IOrderService;
import com.situ.crm.service.IPermissionService;

@Service
public class OrderServiceImpl implements IOrderService {

	@Autowired
	private OrderMapper orderMapper;
	@Autowired
	private SaleChanceMapper saleChanceMapper;

	@Override
	public ServerResponse pageList(Integer page, Integer limit) {
		// 分页
		PageHelper.startPage(page, limit);
		// 查询
		List<Order> list = orderMapper.pageList();
		System.out.println("order list =" + list);
		// 优化
		PageInfo pageInfo = new PageInfo<>(list);
		// 获取总页数
		Integer count = (int) pageInfo.getTotal();

		return ServerResponse.createSuccess("查询成功", count, list);
	}

	@Override
	public ServerResponse insert(Integer id) {
		// 通过id查询出来信息
		SaleChance saleChance = saleChanceMapper.selectByPrimaryKey(id);
		// 把信息赋值给一个order
		Order order = new Order();
		order.setCustomerId(saleChance.getCustomerId());
		order.setSaleChanceId(saleChance.getId());
		// 获取当前时间，转换为时间戳格式，然后赋值给order表中的order_no
		Date time = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmssSSS"); // 以现在的时间作为OrderNo
		String orderNo = sdf.format(time);
		order.setOrderNo(orderNo);
		// 获取现在时间，赋值给下单时间
		order.setOrderDate(time);
		//获取商品id
		order.setProductId(saleChance.getProductId());
		//打印一下 order
		System.out.println("order = "+order);
		// 执行添加
		Integer count = orderMapper.insertSelective(order);
		if (count == 1) {
			return ServerResponse.createSuccess("添加成功");
		} else {
			return ServerResponse.createSuccess("添加失败");
		}
	}
}
